//
//  DatePickerMonthVM.swift
//  kxf
//
//  Created by LNS2 on 2024/3/30.
//

import Foundation

class DatePickerMonthVM: UIView {
    
    var timeConfirmBlock : ((String)->())?
    
    override init(frame:CGRect){
        super.init(frame: CGRect.init(x: 0, y: 0, width: SCREEN_WIDHT, height: SCREEN_HEIGHT))
        self.backgroundColor = UIColor.init(white: 0, alpha: 0.3)
        self.isUserInteractionEnabled = true
        
        initUI()
    }
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    lazy var whiteView : UIView = {
        let vi = UIView()
        vi.backgroundColor = .white
        vi.isUserInteractionEnabled = true
        
        return vi
    }()
    lazy var cancelBtn : UIButton = {
        let btn = UIButton()
        btn.setTitle("取 消", for: .normal)
        btn.setTitleColor(WHColor_16(colorStr: "666666"), for: .normal)
        
        btn.addTarget(self, action: #selector(cancelBtnAction), for: .touchUpInside)
        
        return btn
    }()
    lazy var titleLabel : UILabel = {
        let lab = UILabel()
        lab.text = "开始时间"
        lab.textColor = .COLOR_TEXT_BLACK333
        lab.font = .systemFont(ofSize: 14, weight: .medium)
        
        return lab
    }()
    lazy var confirmBtn : UIButton = {
        let btn = UIButton()
        btn.setTitle("确 定", for: .normal)
        btn.setTitleColor(.THEME, for: .normal)
        
        btn.addTarget(self, action: #selector(confirmBtnAction), for: .touchUpInside)
        
        return btn
    }()
    lazy var pickerView: UIPickerView = {
        let vi = UIPickerView()
        vi.backgroundColor = .clear
        vi.delegate = self
        vi.dataSource = self
        return vi
    }()
    lazy var yearArray: NSArray = {
        var arr = NSMutableArray()
        let year = Int(Date().currentYearNumber as String)
        for i in 2010...(year ?? 2030){
            arr.add(i)
        }
        return arr
    }()
    lazy var monthArray : NSArray = {
        return [1,2,3,4,5,6,7,8,9,10,11,12]
    }()
}

extension DatePickerMonthVM{
    @objc func cancelBtnAction(){
        self.isHidden = true
    }
    @objc func confirmBtnAction(){
        let year = pickerView.selectedRow(inComponent: 0)
        let month = pickerView.selectedRow(inComponent: 1)
        
        let time = "\(yearArray[year])-\(monthArray[month])"
        
        if self.timeConfirmBlock != nil{
            self.timeConfirmBlock!(time)
        }
        
        self.isHidden = true
    }
}

extension DatePickerMonthVM{
    func initUI(){
        addSubview(whiteView)
        whiteView.addSubview(cancelBtn)
        whiteView.addSubview(confirmBtn)
        whiteView.addSubview(pickerView)
        whiteView.addSubview(titleLabel)
        setConstrait()
    }
    
    func setConstrait(){
        whiteView.snp.makeConstraints { (frame) in
            frame.left.bottom.right.width.equalToSuperview()
            frame.height.equalTo(300)
        }
        cancelBtn.snp.makeConstraints { (frame) in
            frame.left.top.equalToSuperview()
            frame.height.equalTo(kFitWidth(40))
            frame.width.equalTo(kFitWidth(100))
        }
        confirmBtn.snp.makeConstraints { (frame) in
            frame.right.top.equalToSuperview()
            frame.height.equalTo(kFitWidth(40))
            frame.width.equalTo(kFitWidth(100))
        }
        titleLabel.snp.makeConstraints { make in
            make.centerX.lessThanOrEqualToSuperview()
            make.centerY.lessThanOrEqualTo(cancelBtn)
        }
        pickerView.snp.makeConstraints { (frame) in
            frame.left.width.equalToSuperview()
            frame.bottom.equalToSuperview()
            frame.top.equalTo(kFitWidth(40))
        }
    }
}


extension DatePickerMonthVM:UIPickerViewDelegate,UIPickerViewDataSource{
    func numberOfComponents(in pickerView: UIPickerView) -> Int {
        return 2
    }
    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        if component == 0 {
            return yearArray.count
        }else{
            return monthArray.count
        }
    }
    func pickerView(_ pickerView: UIPickerView, rowHeightForComponent component: Int) -> CGFloat {
        return kFitWidth(40)
    }
    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
        if component == 0 {
            return "\(yearArray[row]as? Int ?? 0)"
        }else{
            return "\(monthArray[row]as? Int ?? 0)"
        }
    }
//    func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView {
//        let label = UILabel()
//        if component == 0 {
//            label.frame = CGRect.init(x: 0, y: 0, width: SCREEN_WIDHT*0.5, height: kFitWidth(40))
//            label.text = "\(yearArray[row]as? String ?? "")"
//            label.textColor = .COLOR_TEXT_BLACK666
//            label.font = .systemFont(ofSize: 16, weight: .regular)
//        }else{
//            label.frame = CGRect.init(x: SCREEN_WIDHT*0.5, y: 0, width: SCREEN_WIDHT*0.5, height: kFitWidth(40))
//            label.text = "\(yearArray[row]as? String ?? "")"
//            label.textColor = .COLOR_TEXT_BLACK666
//            label.font = .systemFont(ofSize: 16, weight: .regular)
//        }
//        return label
//    }
//    func pickerView(_ pickerView: UIPickerView, widthForComponent component: Int) -> CGFloat {
//        return SCREEN_WIDHT*0.5
//    }
}
